package org.osgroup.controller;

import org.osgroup.project.Restaurant;
import org.osgroup.service.FoodStreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 餐厅监控接口控制器
 */
@RestController
@RequestMapping("/api/monitor/food-street")
@CrossOrigin(origins = "*")
public class MonitorController {
    
    @Autowired
    private FoodStreetService foodStreetService;
    
    /**
     * 获取川菜餐厅订单队列
     */
    @GetMapping("/sichuan-order-queue")
    public ResponseEntity<Map<String, Object>> getSichuanOrderQueue() {
        return getOrderQueue("R001");
    }
    
    /**
     * 获取粤菜餐厅订单队列
     */
    @GetMapping("/cantonese-order-queue")
    public ResponseEntity<Map<String, Object>> getCantonOrderQueue() {
        return getOrderQueue("R002");
    }
    
    /**
     * 获取日料店订单队列
     */
    @GetMapping("/japanese-order-queue")
    public ResponseEntity<Map<String, Object>> getJapaneseOrderQueue() {
        return getOrderQueue("R003");
    }
    
    /**
     * 获取川菜餐厅取餐队列
     */
    @GetMapping("/sichuan-pickup-queue")
    public ResponseEntity<Map<String, Object>> getSichuanPickupQueue() {
        return getPickupQueue("R001");
    }
    
    /**
     * 获取粤菜餐厅取餐队列
     */
    @GetMapping("/cantonese-pickup-queue")
    public ResponseEntity<Map<String, Object>> getCantonPickupQueue() {
        return getPickupQueue("R002");
    }
    
    /**
     * 获取日料店取餐队列
     */
    @GetMapping("/japanese-pickup-queue")
    public ResponseEntity<Map<String, Object>> getJapanesePickupQueue() {
        return getPickupQueue("R003");
    }
    
    /**
     * 通用方法:获取订单队列
     */
    private ResponseEntity<Map<String, Object>> getOrderQueue(String restaurantId) {
        Restaurant restaurant = foodStreetService.getRestaurant(restaurantId);
        if (restaurant == null) {
            return ResponseEntity.notFound().build();
        }
        
        List<Restaurant.Order> orders = restaurant.getOrderQueueSnapshot();
        
        Map<String, Object> response = new HashMap<>();
        response.put("restaurantId", restaurantId);
        response.put("restaurantName", restaurant.getRestaurantName());
        response.put("queueSize", orders.size());
        response.put("queueCapacity", restaurant.getOrderQueueCapacity());
        response.put("orders", orders);
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 通用方法:获取取餐队列
     */
    private ResponseEntity<Map<String, Object>> getPickupQueue(String restaurantId) {
        Restaurant restaurant = foodStreetService.getRestaurant(restaurantId);
        if (restaurant == null) {
            return ResponseEntity.notFound().build();
        }
        
        List<Restaurant.Meal> meals = restaurant.getMealBufferSnapshot();
        
        Map<String, Object> response = new HashMap<>();
        response.put("restaurantId", restaurantId);
        response.put("restaurantName", restaurant.getRestaurantName());
        response.put("bufferSize", meals.size());
        response.put("bufferCapacity", restaurant.getCapacity());
        response.put("meals", meals);
        
        return ResponseEntity.ok(response);
    }
}
